﻿using System;
using System.Collections.Generic;
using System.Text;
using LeePong.Game.Entities;

namespace LeePong.Game.Engines
{
    public class CollisionDetector
    {
        private Engine _engine;
        private ICollisionManager _collisionManager;
        public CollisionDetector(Engine engine, ICollisionManager collisionManager)
        {
            _engine = engine;
            _collisionManager = collisionManager;
        }

        public bool Check()
        {
            var handled = CheckLand();

            if (!handled)
                handled = CheckRightRacket();

            if (!handled)
                handled = CheckLeftRacket();

            return handled;
        }

        private bool CheckLand()
        {
            var ball = _engine.Ball;
            var land = _engine.Land;

            if (ball.Rect.Right > land.Rect.Right)
            {
                if (ball.Angle < -90 || ball.Angle > 90)
                {
                }
                else
                {
                    _collisionManager.OnLandRight();
                    return true;
                }
            }

            if (ball.Rect.Left < land.Rect.Left)
            {
                if (ball.Angle > -90 && ball.Angle < 90)
                {
                }
                else
                {
                    _collisionManager.OnLandLeft();
                    return true;
                }
            }


            if (ball.Rect.Top < land.Rect.Top)
            {
                if (ball.Angle > 0)
                {
                }
                else
                {
                    _collisionManager.OnLandTop();
                    return true;
                }

            }

            if (ball.Rect.Bottom > land.Rect.Bottom)
            {
                if (ball.Angle < 0)
                {
                }
                else
                {
                    _collisionManager.OnLandBottom();
                    return true;
                }
            }

            return false;
        }

        private bool CheckRightRacket()
        {
            var ball = _engine.Ball;
            var rightRacket = _engine.RightRacket;

            if (ball.Angle < -90 || ball.Angle > 90)
            {
            }
            else if (ball.Rect.Right > rightRacket.Rect.Left // ball in
                    && ball.Rect.Left < rightRacket.Rect.Left // but not entirely
                    && ball.Rect.CenterY > rightRacket.Rect.Top && ball.Rect.CenterY < rightRacket.Rect.Bottom) // on the raquet with tolerance
            {
                _collisionManager.OnRightRacket();
                return true;
            }

            return false;
        }

        private bool CheckLeftRacket()
        {
            var ball = _engine.Ball;
            var leftRacket = _engine.LeftRacket;

            if (ball.Angle > -90 && ball.Angle < 90)
            {

            }
            else if (ball.Rect.Left < leftRacket.Rect.Right // ball in
                  && ball.Rect.Right > leftRacket.Rect.Right // but not entirely
                  && ball.Rect.CenterY > leftRacket.Rect.Top && ball.Rect.CenterY < leftRacket.Rect.Bottom) // on the raquet with tolerance
            {
                _collisionManager.OnLeftRacket();
                return true;
            }
            return false;
        }
    }
}
